package main.SQL;

import main.MainApp;

import java.sql.*;

public class SQLTaskRsrc {

    private int taskRsrcId, taskId, rsrcId, factRsrcId, projectId;
    private String rsrcName, rsrcType, rsrcShortName, editMarker, dbName, uom;
    private double plannedUnits, factUnits;

    public SQLTaskRsrc( int taskRsrcId, int taskId, int rsrcId, int factRsrcId, String rsrcName, String rsrcType, String rsrcShortName, double plannedUnits, String editMarker, String dbName, int projectId) {

        this.taskRsrcId = taskRsrcId;
        this.taskId = taskId;
        this.rsrcId = rsrcId;
        this.factRsrcId = factRsrcId;
        this.rsrcName = rsrcName;
        this.rsrcType= rsrcType;
        this.rsrcShortName = rsrcShortName;
        this.plannedUnits = plannedUnits;
        this.editMarker = editMarker;
        this.dbName = dbName;
        this.projectId = projectId;
    }

    public SQLTaskRsrc( int taskRsrcId, int taskId, int rsrcId, int factRsrcId, String rsrcName, String rsrcType, String rsrcShortName, double plannedUnits, String editMarker, String dbName, int projectId, String uom) {

        this.taskRsrcId = taskRsrcId;
        this.taskId = taskId;
        this.rsrcId = rsrcId;
        this.factRsrcId = factRsrcId;
        this.rsrcName = rsrcName;
        this.rsrcType= rsrcType;
        this.rsrcShortName = rsrcShortName;
        this.plannedUnits = plannedUnits;
        this.editMarker = editMarker;
        this.dbName = dbName;
        this.projectId = projectId;
        this.uom = uom;
    }

    public SQLTaskRsrc(ResultSet rs) throws SQLException {

        this.taskRsrcId = rs.getInt("task_rsrc_id");
        this.taskId = rs.getInt("task_id");
        this.rsrcId = rs.getInt( "rsrc_id" );
        this.factRsrcId = rs.getInt( "fact_rsrc_id" );
        this.rsrcName = rs.getString( "rsrc_name" );
        this.rsrcType= rs.getString( "rsrc_type" );
        this.rsrcShortName = rs.getString( "rsrc_short_name" );
        this.plannedUnits = rs.getDouble( "task_rsrc_planned_units" );
        this.editMarker = rs.getString("date_update");
        this.dbName = MainApp.curProject.getDbName();
        this.projectId = MainApp.curProject.getId();
        this.uom = rs.getString("uom");
    }


    public SQLTaskRsrc( int taskRsrcId, int taskId, int rsrcId, double plannedUnits, String editMarker, String dbName, int projectId ) {

        this.taskRsrcId = taskRsrcId;
        this.taskId = taskId;
        this.rsrcId = rsrcId;
        this.plannedUnits = plannedUnits;
        this.editMarker = editMarker;
        this.dbName = dbName;
        this.projectId = projectId;

    }

    public SQLTaskRsrc( int taskRsrcId, int taskId, int rsrcId, double plannedUnits, String editMarker, String dbName, int projectId, String rsrcShortName ) {

        this.taskRsrcId = taskRsrcId;
        this.taskId = taskId;
        this.rsrcId = rsrcId;
        this.plannedUnits = plannedUnits;
        this.editMarker = editMarker;
        this.dbName = dbName;
        this.projectId = projectId;
        this.rsrcShortName = rsrcShortName;

    }

    public void updateSQL () {
        try {
            Connection connSQL = MsSQLConnect.ConnectDB("Factage");
            CallableStatement  stmt = connSQL.prepareCall("{call updateTaskRsrc(?, ?, ?, ?, ?, ?)}");
            stmt.setString(1, dbName);
            stmt.setInt(2, projectId);
            stmt.setInt(3, taskId);
            stmt.setInt(4, rsrcId);
            stmt.setFloat(5, (float) plannedUnits);
            stmt.registerOutParameter(6, Types.INTEGER);
            stmt.executeUpdate();

            int ret = stmt.getInt(6);
            if ( ret != 0 )  this.factRsrcId = ret;

            stmt.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    public void setDbName (String dbName) {this.dbName = dbName;}

    public void setProjectId (int projectId) {this.projectId = projectId;}

    public String getDbName () {return dbName;}

    public int getProjectId () {return projectId;}

    public int getTaskRsrcId () { return taskRsrcId; }

    public void setTaskRsrcId (int taskRsrcId ) { this.taskRsrcId =  taskRsrcId; }

    public int getTaskId () { return taskId; }

    public int getRsrcId () { return rsrcId; }

    public double getPlannedUnits() { return plannedUnits; }

    public String getEditMarker() { return editMarker; }

    public void setEditMarker( String editMarker ) { this.editMarker = editMarker; }

    public int getFactRsrcId() { return factRsrcId; }

    public void setFactRsrcId(int factRsrcId) { this.factRsrcId = factRsrcId; }

    public String getRsrcName() { return rsrcName; }

    public String getRsrcType() { return rsrcType; }

    public String getRsrcShortName() { return rsrcShortName; }

    public void setPlannedUnits(Double plannedUnits) { this.plannedUnits = plannedUnits; }

    public double getFactUnits() {
        return factUnits;
    }

    public void setFactUnits(double factUnits) {
        this.factUnits = factUnits;
    }

    public String getUom() {
        return uom;
    }

    public void setUom(String uom) {
        this.uom = uom;
    }
}

